<!-- 打包复核详情 -->
<template>
	<view>
		<scan></scan>
		<scanner></scanner>
		<sm></sm>
		<honeywell></honeywell>
		<u-search placeholder="请输入商品国标码搜索" :clearabled="true" v-model="keyword" border-color="#606266" @focus="getKeyWordFocus" @custom="getBHByGoodsNumber" ></u-search>
		
		<view style="color:#19be6b">
			<u-icon name="order" size="30" labelSize="15px" labelColor="#19be6b" label="单号" color="#19be6b"></u-icon>：<span>{{this.trackNumber}}</span>
		</view>
		<view style="color:#19be6b">
			<u-icon name="grid" size="30" labelSize="15px" labelColor="#19be6b" label="类型" color="#19be6b"></u-icon>：<span >{{this.orderType}}</span>
		</view>
		<u-card padding="16" v-for="(item,index) in dataList" :key="index">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="12">
							<view class="weikaishi3 lanse">
								商品名称：<span>{{item.goodsName}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="8">
							<view class="weikaishi3 lanse">
								商品编码：<span>{{item.goodsCode}}</span>
							</view>
							<view class="weikaishi3 lanse">
								商品国标码：<span>{{item.goodsNum}}</span>
							</view>
							
						</u-col>
						<u-col span="2">
							<view >
								<u-row>数量<span style="color: #ff9900;font-size: 35px;">{{item.goodsCount}}</span>件</u-row>
							</view>
						</u-col>
						<u-col span="2">
							<view >
								<u-row><u-button type="primary" size="mini" @click="packReviewGoods(item)">复核</u-button></u-row>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			
		</u-card>
		<view>
			<u-row>
				<u-button  type="error" v-if="this.orderType.includes('多品')" @click="revokePackReviewGoods()">撤销</u-button>
			</u-row>
		</view>
		<view>
			<u-calendar v-if="false"  v-model="show" mode="date" max-date="2070-01-01" @change="change" ></u-calendar>
		</view>
		
		<view>
			<u-toast ref="uToast" />
		</view>
		
	</view>
</template>
<script>
	import scan from "@/components/uni-scan/scan.vue"
	import scanner from "@/components/uni-scan/scanner.vue"
	import sm from "@/components/uni-scan/sm.vue"
	import honeywell from "@/components/uni-scan/honeywell.vue"
	import radio from "@/components/uni-scan/radio.js"
	export default {
		components: {
			scanner,
			scan,
			sm,
			honeywell
		},
		data() {
			return {
				setMentiontime:'',
				dataList: [],
				dataList2: [],
				show:false,
				popupShow:false,
				areanumberlist:[],
				goodsNumber:'',
				trackNumber:'',
				pickNumber:'',
				orderType:'',
				goodsList:[],
				keyword:'',
				keywordflag:true,
				okCount :0
			}
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			console.log(99999999999)
			console.log(open)
			this.trackNumber = open.trackNumber
			this.orderType = open.orderType
			this.pickNumber = open.pickNumber
			this.refresh()
			
		},
		onHide() {
		  // 移除所有全局事件监听器
		  uni.$off('scan');
		  uni.$off('scanner');
		  uni.$off('sm');
		  uni.$off('honeywell');
		},
		
		filters:{
			formatDate(value) {
			      if (!value) return ''; // 如果没有值，返回空字符串
			      const date = new Date(value); // 将日期字符串转换为 Date 对象
			      const year = date.getFullYear(); // 获取年份
			      const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份，+1 是因为月份从 0 开始
			      const day = String(date.getDate()).padStart(2, '0'); // 获取日期
			      return `${year}-${month}-${day}`; // 返回格式化的日期字符串
			    }

		},
		onShow() {
			console.log(1111111111111111111111)
			uni.$off('scan');
			uni.$on('scan', (res) => {
				console.log('扫码结果1：', res.codeStr);
				if(this.keywordflag){
					this.keyword = res.codeStr;
					this.scan69(this.keyword);
				}else{
					this.goodsNumber = res.codeStr
					this.searchGoods(this.goodsNumber)
				}
			});
		
			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', (res) => {
				console.log('扫码结果2：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					this.scan69(this.keyword);
				}else{
					this.goodsNumber = res.code
					this.searchGoods(this.goodsNumber)
				}
			})
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', (res) => {
				console.log('扫码结果3：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					this.scan69(this.keyword);
				}else{
					this.goodsNumber = res.code
					this.searchGoods(this.goodsNumber)
				}
			})
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', (res) => {
				console.log('扫码结果4：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					this.scan69(this.keyword);
				}else{
					this.goodsNumber = res.code
					this.searchGoods(this.goodsNumber)
				}
			})
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		methods: {
			//补货
			toBh(item){
				console.log(item)
				this.$Router.push({
					name: 'replenishGoods',
					params: {
						id: item.id
					},
				});
			},
			getKeyWordFocus(){
				this.keywordflag = true;
				console.log(11111)
			},
			getBHByGoodsNumber(value){
				 console.log(123)
				 console.log(value)
				 let dataList1 = [];
				for(let item of this.dataList) {
					if(item.goodsNumber===value){
						dataList1.push(item)
					}
				}
				this.dataList = dataList1
				if(value == null || value == '' ){
					this.refresh()
				}
			},
			checkboxChange(e){
				console.log(e)
			},
			refresh(){
				uni.showLoading({
					title: '加载中...'
				})
				this.$http.post('/rest/pack/packDetailList',{
				},{
					params: {
						trackNumber : this.trackNumber,
						reviewStatus:'未复核'
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					
					this.dataList = res.data.data
					console.log(this.dataList)
					uni.hideLoading()
				
				})
				
			},
			refreshRemain(){
				uni.showLoading({
					title: '加载中...'
				})
				this.$http.post('/rest/pack/packDetailList',{
				},{
					params: {
						trackNumber : this.trackNumber,
						reviewStatus:'未复核'
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList = res.data.data
					uni.hideLoading()
					console.log(888888)
					console.log(this.dataList.length)
					if(this.dataList.length == 0){
					//修改运单状态
					this.updatePackReview()
					}
				})
				
			},
			// 商品复核
			packReviewGoods(item){
				console.log("商品复核")
				console.log(item)
				this.$http.post('/rest/pack/packReviewGoods',{
				},{
					params: {
						id:item.id,
						orderType:this.orderType,
						trackNumber:this.trackNumber,
						pickNumber : this.pickNumber,
						goodsCode:item.goodsCode,
						goodsNum:item.goodsNum
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList = res.data.data
					uni.hideLoading()
					this.refreshRemain()
					radio.play_ding_success()
					
				})
			},
			
			// 撤销商品复核
			revokePackReviewGoods(){
				console.log("撤销商品复核")
				console.log(this.orderType)
				this.$http.post('/rest/pack/revokePackReviewGoods',{
				},{
					params: {
						orderType:this.orderType,
						trackNumber:this.trackNumber,
						pickNumber : this.pickNumber
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList = res.data.data
					uni.hideLoading()
					radio.play_ding_success()
					//跳转打包复核页面
					this.$Router.replace({
						name: 'packReview'
				})
			})
			},
			//根据69码过滤商品
			scan69(code){
				console.log("根据69码过滤商品")
				console.log(code)
				this.$http.post('/rest/pack/packDetailList',{
				},{
					params: {
						trackNumber : this.trackNumber,
						reviewStatus:'未复核'
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					
					
					this.dataList = res.data.data
					console.log(this.dataList)
					uni.hideLoading()
				// this.dataList = []
				this.dataList2 = []
				this.okCount = 0
				for(let item of this.dataList) {
					console.log(item)
					if(item.goodsNum===code){
						console.log(item)
						// this.packReviewGoods(item);
						this.okCount++
						this.dataList2.push(item)
					}
				}
				
				if(this.okCount == 1){
					if(this.dataList2[0].goodsCount == 1){
						this.packReviewGoods(this.dataList2[0]);
					}
				}
				if(this.okCount >0){
					this.$refs.uToast.show({
						title: '成功',
						type: 'success'
					})
					this.dataList = this.dataList2
					radio.play_ding_success();
				}else{
					this.$refs.uToast.show({
						title: '请检查商品！',
						type: 'error'
					})
					this.dataList = [];
					radio.play_ding_cuole();
				}
				})
			},
			//更新主表状态
			updatePackReview(){
				uni.showLoading({
					title: '加载中...'
				})
				this.$http.post('/rest/pack/updatePackReview',{
				},{
					params: {
						trackNumber : this.trackNumber,
						pickNumber : this.pickNumber,
						orderType : this.orderType
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
						//修改运单状态
						this.$Router.replace({
							name: 'packReview'
						});
				})
			},
		},
	}
</script>
<style lang="scss">
	.huanhang{
		word-break:break-all;
	}
</style>